package com.kuaimacode.kframework.admin.services.role;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminMapRolePermissionMapper;
import com.kuaimacode.kframework.mybatis.mapper.admin.SysAdminRoleMapper;
import com.kuaimacode.kframework.mybatis.models.admin.SysAdminRole;

/**
 * 角色信息
 */
@Service
public class  SysAdminRoleService extends ServiceImpl<SysAdminRoleMapper, SysAdminRole> {


	@Autowired
	private SysAdminMapRolePermissionMapper sysMapRolePermissionMapper;
	
	/**
	 * 新建角色
	 * @param role
	 */
	public int createRole(SysAdminRole role) {
		return baseMapper.insert(role);
	}
	
	
	/**
	 * 分页查找所有的角色
	 *
	 * @param pageNum  当前页码，从1开始
	 * @param pageSize 一页显示多少行
	 * @return
	 */
	public PageInfo<SysAdminRole> findAll(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<SysAdminRole> roles = baseMapper.selectList(null);
		return new PageInfo<SysAdminRole>(roles);
	}

	/**
	 * 给角色分配权限
	 *
	 * @param id          角色ID
	 * @param permissionIds 权限ids
	 */
	public void grant(String id, String[] permissionIds) {
		sysMapRolePermissionMapper.grant(id,permissionIds);
	}
	
	/**
	 * 删除角色绑定的全部权限
	 * @param role_id
	 */
	public void removeAllPermissionByUserId(String role_id) {
        
        Map<String, Object> columnMap = new HashMap<String, Object>();
        columnMap.put("role_id", role_id);
		
		sysMapRolePermissionMapper.deleteByMap(columnMap);
	}
	
}

